<?php

namespace app\admin\model;

use think\Model;

/**
 * 位置表
 * @package app\admin\model
 */
class Location extends Model
{
    /**
     * 获取省
     * @param $field
     * @return array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getProvince($field = '*')
    {
        $data = self::where('type', 1)->field($field)->select();
        $data = empty($data) ? [] : $data->toArray();
        return $data;
    }

    /**
     * 获取市
     * @param $pid
     * @param $field
     * @return array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCity($pid, $field = '*')
    {
        $data = self::where('type', 2)->where('pid', $pid)->field($field)->select();
        $data = empty($data) ? [] : $data->toArray();
        return $data;
    }

    /**
     * 获取区
     * @param $pid
     * @param $field
     * @return array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getArea($pid, $field = '*')
    {
        $data = self::where('type', 3)->where('pid', $pid)->field($field)->select();
        $data = empty($data) ? [] : $data->toArray();
        return $data;
    }

    /**
     * 获取位置值
     * @param $where
     * @return mixed
     */
    public static function getLocationValue($where)
    {
        return self::where($where)->value('name');
    }

}